<template>
  <van-popup
    :value="value"
    @click-close-icon="$emit('input', false)"
    closeable
    position="left"
    @closed="isEdit=false"

  >
    <div class="article-channel">
      <div class="head">
        <h3>我的频道<small>点击进入频道</small></h3>
        <a class="edit" href="javascript:;" @click="isEdit=!isEdit" :class="{active:isEdit}" >{{isEdit?"完成":"编辑"}}</a>
      </div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="(item, index) in channellist"
          :key="item.id"
          :class="{ active: activeIndex === index }"
          @click="enterChannel(index)"
          >{{ item.name }}
              <geek-icon v-show="isEdit && index!==0 && index!== activeIndex" name="tag-close" @click.native.stop='delChannel(item.id)'></geek-icon>
        </a>
      </div>
      <div class="head" style="margin-top: 12px">
        <h3>频道推荐</h3>
      </div>
      <div class="body">
        <a href="javascript:;" v-for="i in optionalChannels" :key="i.id" @click="addChannel(i)">
          {{ i.name }}</a
        >
      </div>
    </div>
  </van-popup>
</template>
<script>
import { getAllChannels, addChannel, delChannel } from '@/api/channel'
export default {
  name: 'ArticleChannel',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    channellist: {
      type: Array,
      default: () => []
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  created () {
    this.getAllChannels()
  },
  computed: {
    // 可选频道
    optionalChannels () {
      // 在myChannels中找不到的就是可选的
      return this.allChannels.filter(
        (item) => !(this.channellist.find((i) => i.id === item.id))
      )
    }
  },
  methods: {
    async delChannel (id) {
      await delChannel(id)
      const index = this.channellist.findIndex(item => item.id === id)
      this.channellist.splice(index, 1)
    },
    async addChannel (channel) {
      const newMyChannels = []
      this.channellist.forEach((item, index) => {
        newMyChannels.push({
          id: item.id,
          name: item.name,
          seq: index
        })
      })
      newMyChannels.push({ id: channel.id, name: channel.name, seq: newMyChannels.length + 1 })
      await addChannel(newMyChannels)

      this.channellist.push(channel)
    },
    // 得到所有的channels
    async getAllChannels () {
      const [err, data] = await getAllChannels()
      if (err) return this.$toast.fail(err)
      console.log('全部channels调用成功', data)
      this.allChannels = data.data.data.channels
      console.log(this.allChannels)
    },
    enterChannel (index) {
      this.$emit('input', false)
      this.$emit('update:activeIndex', index)
    }
  }
}
</script>
<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
}
.article-channel {
  margin-top: 44px;
}
.article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #de644b;
      border-radius: 11px;
      border: 1px solid #de644b;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #de644b;
      }
    }
  }
  .body {
    padding: 5px 6px 0 16px;
    a {
      display: inline-block;
      padding: 0 8px;
      font-size: 14px;
      color: #3a3948;
      background: #f7f8fa;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
           position: relative;
     .geek-icon {
        position: absolute;
        top: -5px;
        right: -5px;
        line-height: 1;
     }
      &.active {
        color: @geek-color;
      }
    }
  }
}
</style>
